package com.shuoyue.ycgk.ui.mycourse;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.appcatch.SPUtils;
import com.shuoyue.ycgk.base.BaseMvpFragment;
import com.shuoyue.ycgk.base.ListWithPage;
import com.shuoyue.ycgk.base.baseadapter.IndexFragmentStateAdapter;
import com.shuoyue.ycgk.entity.Course;
import com.shuoyue.ycgk.entity.Type;
import com.shuoyue.ycgk.event.SwitchBaseEvent;
import com.shuoyue.ycgk.ui.common.CommonModuleContract;
import com.shuoyue.ycgk.ui.course.CourseAdapter;
import com.shuoyue.ycgk.ui.course.FragmentCourseTab;
import com.shuoyue.ycgk.ui.course.info.CourseInfoActivity;
import com.shuoyue.ycgk.utils.SizeUtil;
import com.shuoyue.ycgk.utils.SpacesItemDecoration;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class FramentMyCourseAll extends BaseMvpFragment<MyCourseContract.Presenter> implements MyCourseContract.View, CommonModuleContract.View {
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.refreshlayout)
    SmartRefreshLayout refreshlayout;
    CourseAdapter adapter;
    ListWithPage listWithPage;
    CommonModuleContract.Presenter modulePresenter;
    List<Type> baseTypes;
//    Type baseType;

    @Override
    public void initParams(Bundle bundle) {

    }

    public static FramentMyCourseAll getInstance() {
        FramentMyCourseAll fragment = new FramentMyCourseAll();
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_sigle_list;
    }

    @Override
    protected void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.addItemDecoration(new SpacesItemDecoration(SizeUtil.dip2px(mContext, 12), false));
        adapter = new CourseAdapter(null);
        recyclerView.setAdapter(adapter);
        refreshlayout.setEnableLoadMore(false);

        refreshlayout.setOnRefreshListener(refreshLayout -> {
//            mPresenter.getAllCourse(0, baseType.getId(), 1);
            getCourseData(0, 1);


            refreshLayout.finishRefresh();
        });
        refreshlayout.setOnLoadMoreListener(refreshLayout -> {
//            mPresenter.getAllCourse(0, baseType.getId(), listWithPage.getCurrent() + 1);
            getCourseData(0, listWithPage.getCurrent() + 1);
            refreshLayout.finishLoadMore();
        });

        adapter.setOnItemChildClickListener((adapter, view, position) -> {
            Course course = (Course) adapter.getItem(position);
            switch (view.getId()) {
                case R.id.lay_all:
                    startActivity(new Intent(mContext, CourseInfoActivity.class)
                            .putExtra("id", course.getId()));
                    break;
            }
        });
        registEmptyView(adapter);
        registEmptyView(adapter, R.layout.layout_empty_course);

    }

    @Override
    protected void initData() {
        super.initData();
        modulePresenter = new CommonModuleContract.Presenter();
        modulePresenter.attachView(this);
        mPresenter = new MyCourseContract.Presenter();
        mPresenter.attachView(this);
        modulePresenter.getBaseModels(2);


    }

    @Override
    public void getMyCourseSuc(List<Course> courses) {
        adapter.resetData(courses);
        refreshlayout.finishRefresh();
        refreshlayout.finishLoadMore();
    }

    @Override
    public void getAllCourseSuc(ListWithPage<Course> list) {
        listWithPage = list;
        if (list.getCurrent() == 1) {
            adapter.resetData(list.getRecords());
        } else {
            adapter.addData(list.getRecords());
        }
        refreshlayout.setEnableLoadMore(list.getCurrent() != list.getPages() && list.getPages() > 0);
    }

    @Override
    public String getTitle() {
        return "全部课程";
    }

    @Override
    public void setBaseType(List<Type> baseTypes) {
        this.baseTypes = baseTypes;

        getCourseData(0, 1);

    }

    private void getCourseData(int type, int page) {
        if (baseTypes == null || baseTypes.size() == 0) {
            modulePresenter.getBaseModels(2);
            return;
        }
        String perName = SPUtils.getLastSelectMoudle(mContext);
        Type tagetType = null;
        if (perName != null) {
            for (Type baseType : baseTypes) {
                if (perName.equals(baseType.getName())) {
                    tagetType = baseType;
                }
            }
        } else {
            tagetType = baseTypes.get(0);
        }
        mPresenter.getAllCourse(type, tagetType.getId(), page);
    }

    @Override
    public void setTabType(List<Type> type) {

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        EventBus.getDefault().unregister(this);
        super.onDestroyView();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void switchBaseTypeEvent(SwitchBaseEvent s) {
        getCourseData(0, 1);
    }

}
